//
//  XSquareBottomView.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/16.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit
@objc
/// 按钮点击代理协议
public protocol XSquareCellBottomViewDelegate: NSObjectProtocol {
    /// 选中转发
    ///
    /// -
    @objc optional func bottomDidSelectedRetweet()
    /// 选中评论
    ///
    ///
    @objc optional func bottomDidSelectedComment()
    /// 选中赞
    ///
    @objc optional func bottomDidSelectedLike()
}
class XSquareCellBottomView: UIView {

    var viewModel:XSquareNewsViewModel?{
        didSet{
            //FIXME:设置底部文字
            retweetedButton.setTitle(viewModel?.squareNews?.forwardNum, for: .normal)
            commentButton.setTitle(viewModel?.squareNews?.commentNum, for: .normal)
            let likeCount = viewModel?.squareNews?.likeArray?.count ?? 0
            likeButton.setTitle(likeCount>0 ? "\(likeCount)":"赞", for: .normal)
            checkIfLike()
        }
    }
    weak var delegate:XSquareCellBottomViewDelegate?
    /// 转发
    lazy var retweetedButton: UIButton = UIButton.cz_textButton("转发", fontSize: 12, normalColor: UIColor.cz_color(withHex: 0x828282), highlightedColor: UIColor.orange)
    lazy var bottom_line_1:UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    /// 评论
    lazy var commentButton: UIButton = UIButton.cz_textButton("评论", fontSize: 12, normalColor: UIColor.cz_color(withHex: 0x828282), highlightedColor: UIColor.orange)
    lazy var bottom_line_2:UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    /// 赞
    lazy var likeButton: UIButton! = UIButton.cz_textButton("赞", fontSize: 12, normalColor: UIColor.cz_color(withHex: 0x828282), highlightedColor: UIColor.orange)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    /// 检查是否是自己点过赞的
    func checkIfLike(){
        for like in viewModel?.squareNews?.likeArray ?? []{
            if like.userId == 718900{
                likeButton.isSelected = true
                return
            }
        }
        likeButton.isSelected = false
    }
    /// 按钮点击事件
    ///
    /// - Parameter btn: <#btn description#>
    func clickBtn(btn:UIButton){
        let tag = btn.tag
        switch tag {
        case 0:
            delegate?.bottomDidSelectedRetweet?()
        case 1:
            delegate?.bottomDidSelectedComment?()
        case 2:
            delegate?.bottomDidSelectedLike?()
        default: break
            
        }
    
//        print("select in tag:\(tag)")
    
    }
}
extension XSquareCellBottomView{
    ///设置UI
    fileprivate func setupUI(){
        addSubview(retweetedButton)
        retweetedButton.tag = 0
        retweetedButton.setImage(UIImage(named:"timeline_icon_retweet"), for: .normal)
        retweetedButton.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        retweetedButton.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        addSubview(commentButton)
        commentButton.tag = 1
        commentButton.setImage(UIImage(named:"timeline_icon_comment"), for: .normal)
        commentButton.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        commentButton.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        addSubview(likeButton)
        likeButton.setImage(UIImage(named:"timeline_icon_unlike"), for: .normal)
        likeButton.setImage(UIImage(named:"timeline_icon_like"), for: .selected)
        likeButton.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        likeButton.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        likeButton.tag = 2
        addSubview(bottom_line_1)
        addSubview(bottom_line_2)
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //转发按钮
        retweetedButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(snp.centerY)
            make.left.equalTo(snp.left)
            make.height.equalTo(snp.height)
        }
        //分隔线1
        bottom_line_1.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(snp.centerY)
            make.left.equalTo(retweetedButton.snp.right)
            make.width.equalTo(1)
            make.height.equalTo(retweetedButton.snp.height)
            
        }
        //评论按钮
        commentButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(snp.centerY)
            make.left.equalTo(retweetedButton.snp.right)
            make.size.equalTo(retweetedButton.snp.size)
            make.right.equalTo(likeButton.snp.left)
            
        }
        //分隔线2
        bottom_line_2.snp.makeConstraints { (make) in
            make.centerY.equalTo(snp.centerY)
            make.left.equalTo(commentButton.snp.right)
            
            make.size.equalTo(bottom_line_1.snp.size)
        }
        //赞
        likeButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(snp.centerY)
            make.right.equalTo(snp.right)
            make.size.equalTo(retweetedButton.snp.size)
        }
        
        
        
        
        
    }
}
